<?php
$IN_defaultpl="maps";//模版文件
include (dirname(__FILE__)."/../index.inc.php");//全局变量

$web_userId=trim(isset($_SESSION['web_user_uid'])?$fun->authcode($_SESSION['web_user_uid'], 'DECODE', $pwdconstant,$expiry =0):"");
if($web_userId){
	$userInfo=$db->get_one("SELECT u.`uid`, u.`status`, u.`uname`, u.`realname`, u.`ncname`, u.`sex`, u.`mobile`, u.`tel`, u.`email`, u.`qq`, u.`addtime`, u.`lastlogintime`, u.`logintime`, u.`is_valid`, u.`login_num`, u.`modify_ip`,t.`id` FROM `{$tbprefix}user` AS u LEFT JOIN `{$tbprefix}user_token` AS t ON t.uid=u.uid WHERE u.`uid`='{$web_userId}' AND 1");
}else{
	$userInfo=array("uid"=>0,"uname"=>"Unknown");
}
$smarty->assign("userInfo",$userInfo);

//广告位
$Advertising=$db->get_all("SELECT `id`,`img_name`,`img_url`,`img_src` FROM `{$tbprefix}ad_img` WHERE `is_show`='1' AND type=1 AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC LIMIT 0,5");
$smarty->assign("Advertising",$Advertising);

//是否登录判断
if(empty($web_userId)){
	//只允许登录的会员
	echo "<script  type='text/javascript' language='javascript'>window.location.href='../".$list_url['login']."';</script>";
	exit();
}

switch ($mod){
	case "index"://首页
		
		$province_get=trim(empty($_REQUEST['province'])?"0":$fun->checkInput($_REQUEST['province']));
		$smarty->assign("province_get",$province_get);
		$keyword=trim(empty($_REQUEST['keyword'])?"":$fun->checkInput($_REQUEST['keyword']));
		$smarty->assign("keyword",$fun->showInput($keyword, ENT_QUOTES, "UTF-8" ));
		$choice_get=trim(empty($_REQUEST['choice'])?"0":$fun->checkInput($_REQUEST['choice']));
		$smarty->assign("choice_get",$choice_get);
		// 查询区域
		$province = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province", $province );
		
		//区域
		$province1 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='1' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province1", $province1 );
		$province2 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='2' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province2", $province2 );
		$province3 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='3' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province3", $province3 );
		$province4 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='4' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province4", $province4 );
		$province5 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='5' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province5", $province5 );
		$province6 = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND `belong_to`='6' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province6", $province6 );
	
		$smarty->display("index".$list_url['suffix_tpl']);
		break;
	case "index_list"://列表信息
		$condition="";
		/*分页数*/
		$pagesize =20;
		//当前页
		$page =empty($_REQUEST['page'])||($_REQUEST['page']<0)?"1":trim($_REQUEST['page']);
		
		$province_get=trim(empty($_REQUEST['province'])?"0":$fun->checkInput($_REQUEST['province']));
		$city_get=trim(empty($_REQUEST['city'])?"0":$fun->checkInput($_REQUEST['city']));
		$keyword=trim(empty($_REQUEST['keyword'])?"":$fun->checkInput($_REQUEST['keyword']));
		$choice_get=trim(empty($_REQUEST['choice'])?"0":$fun->checkInput($_REQUEST['choice']));
		
		if($keyword){
			$keywords = str_replace(" ","%",trim(urldecode($keyword)));
			switch ($choice_get){
				case 0://综合搜索
					$condition.="((s.`merchant_name` LIKE '%".$keywords."%')OR(s.`address` LIKE '%".$keywords."%')OR(s.`mobile` LIKE '%".$keywords."%')OR(s.`tel` LIKE '%".$keywords."%')) AND ";
					break;
				case 1:
					$condition.="(s.`address` LIKE '%".$keywords."%') AND ";
					break;
				case 2:
					$condition.="((s.`merchant_name` LIKE '%".$keywords."%')) AND ";		
					break;
				case 3:
					$condition.="((s.`mobile` LIKE '%".$keywords."%')OR(s.`tel` LIKE '%".$keywords."%')) AND ";
					break;
				default:
					$condition.="((s.`merchant_name` LIKE '%".$keywords."%')OR(s.`address` LIKE '%".$keywords."%')OR(s.`mobile` LIKE '%".$keywords."%')OR(s.`tel` LIKE '%".$keywords."%')) AND ";
					break;
			}
		}
		$smarty->assign("keyword",$fun->showInput($keyword, ENT_QUOTES, "UTF-8" ));
		
		// 查询区域
		$province = $db->get_all ( "SELECT infoid, title FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province", $province );
		if ($province_get) {
			$sql_city = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `parentid`='".$province_get."' AND `ishow`='1' AND  `is_valid`='1' AND 1 ORDER BY `orderid` DESC";
			$cityList = $db->get_all ( $sql_city );
			$smarty->assign ( "cityList", $cityList ); // 市
			$condition .= " (s.`province` ='".$province_get."') AND ";
			
			$province_show = $db->get_one ( "SELECT infoid, title FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `is_valid`='1' AND `infoid`='{$province_get}' AND 1 ORDER BY `orderid` DESC" );
			
		}else{
			if(empty($keyword)){
				$condition .= " (s.`id` ='0') AND ";//必须有省
				$province_show['title']="暂无";
			}
		}
		if ($city_get) {
			$condition .= " (s.`city`='".$city_get."') AND ";
		}
		$smarty->assign("provinceName",$province_show['title']);
		$smarty->assign("province_get",$province_get);
		$smarty->assign("city_get",$city_get);
		$smarty->assign("choice_get",$choice_get);
		
		$fpage = $fun->setPageUri();//该页面;
		
		$mer_sql="SELECT s.`id`, s.`merchant_name`, s.`merchant_no`, s.`province`, s.`city`, s.`area`,
		s.`tel`, s.`mobile`, s.`qq`, s.`contacts`, s.`is_show`, s.`e_mail`,s.`lat`,s.`lng`,
		s.`is_valid`, s.`addtime`, s.`change_time`, s.`del_time`,a1.`title` AS `province_value`,
		a2.`title` AS `city_value`,a3.`title` AS `area_value`
		FROM `{$tbprefix}service` AS s
		LEFT JOIN `{$tbprefix}area` AS a1 ON s.province=a1.infoid
		LEFT JOIN `{$tbprefix}area` AS a2 ON s.city=a2.infoid
		LEFT JOIN `{$tbprefix}area` AS a3 ON s.area=a3.infoid
		WHERE {$condition} s.`is_valid`='1' AND s.`is_show`='1' AND 1 ORDER BY `listorder` DESC,s.`addtime` DESC";
		
		/*--分页开始--*/
		$numpage = $db->pagedeal($mer_sql, $pagesize);//调用分页处理函数
		
		//SQL LIMIT起始
		$startloc=(($page-1)*$pagesize<0)?0:($page-1)*$pagesize;
		
		$merValue=$db->get_all($mer_sql." LIMIT $startloc,$pagesize");
		$i=0;
		$merchant_arr=array();
		foreach ($merValue as $key=>$value){
			//序号
			$i++;$pageid=(($page-1)*$pagesize)+$i;
			$merchant_arr[$key]=$value;
			$merchant_arr[$key]['pageid']=$pageid;
			$merchant_arr[$key]['province_value']=!empty($value['province_value'])?$fun->showInput($value['province_value']):"&nbsp;";
			$merchant_arr[$key]['city_value']=!empty($value['city_value'])?$fun->showInput($value['city_value']):"&nbsp;";
			$merchant_arr[$key]['area_value']=!empty($value['area_value'])?$fun->showInput($value['area_value']):"&nbsp;";
			$merchant_arr[$key]['merchant_name']=!empty($value['merchant_name'])?$fun->showInput($value['merchant_name']):"&nbsp;";
												
		}//print_r($merchant_arr);
		$smarty->assign("merchant_arr",$merchant_arr);
		$act_url=$_SESSION['index_url']=$fun->GetCurUrl(1);//获取URL的路径存入session
		//----------------------分页相关的模板变量替换-------------------------//
		$smarty->assign("recordcount", $recordcount); 	//总记录数
		$smarty->assign("pagecount", $pagecount);  		//总页数
		$smarty->assign("prepage", $page - 1);     		//上一页
		$smarty->assign("nexpage", $page + 1);     		//下一页
		$smarty->assign("numpage", $numpage);    		//数字分页
		$smarty->assign("fpage", $fpage); 		//要分页的网页
		$smarty->assign("page",$page);
		$smarty->assign("pagesize",$pagesize);
		
		@$smarty->display("index_list".$list_url['suffix_tpl']);		
		break;
		
	case "index_maps"://在线地图显示
		
		$id_get=trim(empty($_REQUEST['id'])?"0":$fun->checkInput($_REQUEST['id']));
		if($id_get){
			$service_info_sql="SELECT `id`, `merchant_name`, `merchant_no`, `province`, `city`, `area`, `address`, `tel`, `mobile`, `qq`, `e_mail`, `contacts`,  `lat`, `lng` FROM `{$tbprefix}service` WHERE `id`='{$id_get}' AND `is_valid`='1' AND `is_show`='1' AND 1";
			$service_info=$db->get_one($service_info_sql);
			if($service_info['id']){
		
				if ($service_info['province']) {
					$sql_province_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['province']."' AND `level`='1' AND 1 ORDER BY `orderid` DESC";
					$provinceListValue = $db->get_one ( $sql_province_value );
					$service_info['province_value']=empty($provinceListValue['title'])?"":$fun->showInput($provinceListValue['title']);
				}
				if ($service_info['city']) {
					$sql_city_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['city']."' AND `level`='2' AND 1 ORDER BY `orderid` DESC";
					$cityListValue = $db->get_one ($sql_city_value);
					$service_info['city_value']=empty($cityListValue['title'])?"":$fun->showInput($cityListValue['title']);
				}
				if ($service_info['area']) {
					$sql_area_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['area']."' AND `level`='3' AND 1 ORDER BY `orderid` DESC";
					$areaListValue = $db->get_one ( $sql_area_value );
					$service_info['area_value']=empty($areaListValue['title'])?"":$fun->showInput($areaListValue['title']);
				}
				$service_info['address']=empty($service_info['address'])?"":$fun->showInput($service_info['address']);
		
				$smarty->assign("one",$service_info);
		
				$smarty->display("index_maps".$list_url['suffix_tpl']);
			}else{
				$fun->msg("无效的链接,正在返回中...","",2,$title="错误页面提示",$back_color="f78a12",$link_url="../");
			}
		
		}else{
			$fun->msg("无效的链接,正在返回中...","",2,$title="错误页面提示",$back_color="f78a12",$link_url="../");
		}
		
		break;
	
	case "index_info":
		$id_get=trim(empty($_REQUEST['id'])?"0":$fun->checkInput($_REQUEST['id']));
		if($id_get){
			$service_info_sql="SELECT `id`, `merchant_name`, `merchant_no`, `province`, `city`, `area`, `address`, `tel`, `mobile`, `qq`, `e_mail`, `contacts`, `is_show`, `lat`, `lng`,`addtime`,`change_time`,`content` FROM `{$tbprefix}service` WHERE `id`='{$id_get}' AND `is_valid`='1' AND `is_show`='1' AND 1";
			$service_info=$db->get_one($service_info_sql);
			if($service_info['id']){
				
				$keyword=trim(empty($_REQUEST['keyword'])?"":$fun->checkInput($_REQUEST['keyword']));
				$smarty->assign("keyword",$fun->showInput($keyword, ENT_QUOTES, "UTF-8" ));
				$choice_get=trim(empty($_REQUEST['choice'])?"0":$fun->checkInput($_REQUEST['choice']));
				$smarty->assign("choice_get",$choice_get);
				// 查询区域
				$province = $db->get_all ( "SELECT infoid, title,belong_to FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `ishow`='1' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC" );
				$smarty->assign ( "province", $province );
				
				if ($service_info['province']) {
					$sql_city = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `parentid`='".$service_info['province']."' AND `level`='2' AND `ishow`='1' AND 1 ORDER BY `orderid` DESC";
					$cityList = $db->get_all ( $sql_city );
					$smarty->assign ( "cityList", $cityList ); //市
					
					$sql_province_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['province']."' AND `level`='1' AND 1 ORDER BY `orderid` DESC";
					$provinceListValue = $db->get_one ( $sql_province_value );
					$service_info['province_value']=empty($provinceListValue['title'])?"":$fun->showInput($provinceListValue['title']);
				}
				if ($service_info['city']) {
					$sql_city_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['city']."' AND `level`='2' AND 1 ORDER BY `orderid` DESC";
					$cityListValue = $db->get_one ($sql_city_value);
					$service_info['city_value']=empty($cityListValue['title'])?"":$fun->showInput($cityListValue['title']);
				}
				if ($service_info['area']) {
					$sql_area_value = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `infoid`='".$service_info['area']."' AND `level`='3' AND 1 ORDER BY `orderid` DESC";
					$areaListValue = $db->get_one ( $sql_area_value );
					$service_info['area_value']=empty($areaListValue['title'])?"":$fun->showInput($areaListValue['title']);
				}
				$service_info['addtime']=empty($service_info['addtime'])?"":date("Y年m月d日 H:i",$service_info['addtime']);
				$service_info['change_time']=empty($service_info['change_time'])?"":date("Y年m月d日 H:i",$service_info['change_time']);
				$service_info['address']=empty($service_info['address'])?"":$fun->showInput($service_info['address']);
				$service_info['content']=empty($service_info['content'])?"":trim($service_info['content']);
				
				$smarty->assign("one",$service_info);
				$smarty->assign("province_get",$service_info['province']);
				$smarty->assign("city_get",$service_info['city']);
				$smarty->display("index_info".$list_url['suffix_tpl']);
			}else{
				$fun->msg("无效的链接,正在返回中...","",2,$title="错误页面提示",$back_color="f78a12",$link_url="../");
			}
	
		}else{
			$fun->msg("无效的链接,正在返回中...","",2,$title="错误页面提示",$back_color="f78a12",$link_url="../");
		}
		
		break;

	default:
		$fun->msg("无效的链接,正在返回中...","",2,$title="错误页面提示",$back_color="f78a12",$link_url="../");
		break;
}
?>